/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/*
 * This script is also referenced by the ecommerce's screens and
 * should not contain order component's specific code.
 */
import org.ofbiz.entity.util.EntityUtil;
import org.ofbiz.base.util.*;
import org.ofbiz.product.catalog.*;
import org.ofbiz.product.category.*;
import javolution.util.FastMap;
import javolution.util.FastList;
import javolution.util.FastList.*;
import org.ofbiz.entity.*;
import java.util.List;

// Put the result of CategoryWorker.getRelatedCategories into the separateRootType function as attribute.
// The separateRootType function will return the list of category of given catalog.
// PLEASE NOTE : The structure of the list of separateRootType function is according to the JSON_DATA plugin of the jsTree.
def getAllProducts(String partyId) {
    allGps = 0;
    productRoleList = delegator.findByAnd("ProductRole", ["partyId" : partyId, roleTypeId : "PRODUCT_OWNER"], null , false);
    productRoleList = EntityUtil.filterByDate(productRoleList);
    allGps = productRoleList.size();
    partyRelationshipList = delegator.findByAnd("PartyRelationship", ["partyIdFrom" : partyId], null, false);
    partyRelationshipList = EntityUtil.filterByDate(partyRelationshipList);
    if (partyRelationshipList) {
        partyRelationshipList.each { partyRelationship ->
            allGps = allGps + getAllProducts(partyRelationship.partyIdTo);
        }
    }
    return allGps;
}


completedTree =  FastList.newInstance();
completedTreeContext =  FastList.newInstance();
existParties =  FastList.newInstance();
subtopLists =  FastList.newInstance();
partyId = userLogin.partyId;

//internalOrg list
partyRelationships = EntityUtil.filterByDate(delegator.findByAnd("PartyRelationship", [partyIdFrom : partyId, partyRelationshipTypeId : "GROUP_ROLLUP"], null, false));
//root
partyRoot = delegator.findOne("PartyNameView", [partyId : partyId], false);
partyRootMap = FastMap.newInstance();
partyRootMap.put("partyId", partyId);

name = (partyRoot.getString("firstName") != null ? partyRoot.getString("firstName") : "") +
        (partyRoot.getString("firstName") != null ? " " : "") +
        (partyRoot.getString("lastName") != "" ? partyRoot.getString("lastName") : "") +
        (partyRoot.getString("groupName") != null ? partyRoot.getString("groupName") : "");
productRoleList = EntityUtil.filterByDate(delegator.findByAnd("ProductRole", UtilMisc.toMap(
                                "partyId", partyId, "roleTypeId", "PRODUCT_OWNER"), null, false));
name = name + "(" + productRoleList.size() + "/"+ getAllProducts(partyId) + ")";
partyRootMap.put("name", name);

//child
for(partyRelationship in partyRelationships) {
    partyNameView = delegator.findOne("PartyNameView", [partyId : partyRelationship.getString("partyIdTo")], false);
    partyNameViewMap = FastMap.newInstance();
    partyNameViewMap.put("partyId", partyNameView.getString("partyId"));
    name = (partyNameView.getString("firstName") != null ? partyNameView.getString("firstName") : "") +
            (partyNameView.getString("firstName") != null ? " " : "") +
            (partyNameView.getString("lastName") != null ? partyNameView.getString("lastName") : "") +
            (partyNameView.getString("groupName") != null ? partyNameView.getString("groupName") : "");
    productRoleList = EntityUtil.filterByDate(delegator.findByAnd("ProductRole", UtilMisc.toMap(
                                "partyId", partyRelationship.getString("partyIdTo"), "roleTypeId", "PRODUCT_OWNER"), null, false));
    name = name + "(" + productRoleList.size() + "/" + getAllProducts(partyId) + ")";
    
    partyNameViewMap.put("name", name);
    completedTreeContext.add(partyNameViewMap);

    subtopLists.addAll(partyRelationship.getString("partyIdTo"));
}

partyRootMap.put("child", completedTreeContext);
completedTree.add(partyRootMap);
// The complete tree list for the category tree
context.completedTree = completedTree;
context.subtopLists = subtopLists;
println "========== completedTree : " + completedTree;
println "========== subtopLists : " + subtopLists;
